import {
  showToast,
  requestPayment
} from '../../utils/asyncWx.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';
import {
  request
} from '../../request/index.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address = wx.getStorageSync("address");
    let cart = wx.getStorageSync("cart") || [];
    cart = cart.filter(v => v.checked);
    this.setData({
      address
    });
    let totalNum = 0;
    let totalPrice = 0;
    cart.forEach(v => {
      totalNum += v.num;
      totalPrice += v.num * v.goods_price;
    });
    this.setData({
      cart,
      totalNum,
      totalPrice,
      address
    });
  },

  async handlePay() {
    // try {
    //   const token = wx.getStorageSync("token");
    // if (!token) {
    //   wx.navigateTo({
    //     url: '/pages/auth/index'
    //   });
    //   return;
    // }
    // const order_price = this.data.totalPrice;
    // const consignee_addr = this.data.address.all;
    // const cart = this.data.cart;
    // let goods = [];
    // cart.forEach(v => goods.push({
    //   goods_id: v.goods_id,
    //   goods_number: v.num,
    //   goods_price: v.goods_price
    // }));
    // const orderParams = {
    //   order_price,
    //   consignee_addr,
    //   goods
    // };
    // const {
    //   order_number
    // } = await request({
    //   url: "/my/orders/create",
    //   method: "POST",
    //   data: orderParams
    // });
    // const {
    //   pay
    // } = await request({
    //   url: "/my/orders/req_unifiedorder",
    //   method: "POST",
    //   data: {
    //     order_number
    //   }
    // });
    // //自己的appleID和支付参数不一致，所以requestPayment没有支付权限
    //   await requestPayment(pay);
    //   //查询后台订单状态
    // const res = await request({
    //   url: "/my/orders/chkOrder",
    //   method: "POST",
    //   data: {
    //     order_number
    //   }
    // });
    //   console.log(res);
    //   await showToast({
    //     title: "支付成功"
    //   });
    //   //支付完后，手动删除购物车里支付过的商品，刷新缓存中的cart即可
    //   let Newcart = getStorageSync("cart");
    //   Newcart = Newcart.vilter(v => !v.checked);
    //   wx.setStorageSync("cart",Newcart );
    //   //支付成功，跳转到订单页面
    //   wx.navigateTo({
    //     url: '/pages/order/index',
    //   });
    // }
    //catch (error) {
      await showToast({
        title: "支付失败"
      });
      //console.log(error);
    //}
  }
})